from utils.bilibili import Uploader
import os
import sys
import time
from multiprocessing import Process


def make_sign(task_id, path, vtitle):
    title_prefix = ""
    s = ""
    path = os.path.join(path, "tmpfile.info")

    if os.path.exists(path):
        with open(path, "r", encoding="utf8") as f:
            title_prefix = f.readline().rstrip()
            s = f.read() + "\n"
        ts = task_id / 1000 / 1000 / 1000

        timeArray = time.localtime(ts)
        s = s.format(vtitle, timeArray.tm_year, timeArray.tm_mon, timeArray.tm_mday)

        title_prefix = title_prefix.format(timeArray.tm_mon, timeArray.tm_mday)

        if vtitle[0] == "【":
            vtitle = title_prefix + vtitle[1:]
        else:
            vtitle = title_prefix[0:-1] + "】" + vtitle

    return s, vtitle


def get_info(task_id, max_length):
    basePath = os.path.abspath('.')

    path = os.path.join(basePath, str(task_id))
    video_info = f'{path}/info.txt'

    f = open(video_info, "r", encoding="utf8")
    graph_path = f'{path}/graph.jpg'

    video_title = f.readline().rstrip()
    video_id = f.readline().rstrip()
    f.readline()

    temp, video_title = make_sign(task_id, basePath, video_title)
    video_desc = temp + f.read()

    return video_title[0:79], graph_path, video_desc[0:max_length], f"https://youtu.be/{video_id}"


def getTagAndDynamic():
    basePath = os.path.abspath('.')
    path = os.path.join(basePath, "dynamic.info")
    if os.path.exists(path):
        with open(path, "r", encoding="utf8") as f:
            tag_str = f.readline()
            dynamic_str = f.read()
    return tag_str.rstrip(), dynamic_str.rstrip()


if __name__ == '__main__':
    if len(sys.argv) <= 1:
        taskId = 1612101763047323000
    else:
        taskId = int(sys.argv[1])
    video_title, graph_path, video_desc, video_url = get_info(taskId, 2500)
    tag_str, dynamic_str = getTagAndDynamic()
    print(video_title, 24, tag_str.rstrip(), video_url, graph_path, dynamic_str, sep="\n", end="\n")
    print(video_desc)
